package leetcode.code;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution6 implements IDebug {

	public String convert(String s, int numRows) {
		if (numRows == 1)
			return s;
		int len = s.length();
		char[] c = s.toCharArray();
		StringBuffer ans = new StringBuffer();
		int start = 0;
		while (start < len) {// 第一行
			ans.append(c[start]);
			start = start + ((numRows - 1) << 1);
		}
		for (int i = 1; i < numRows - 1; i++) {// 中间多行
			start = 0;
			while (start < len) {
				int index1 = start + i;
				if (index1 < len) {
					ans.append(c[index1]);
				}
				start = start + ((numRows - 1) << 1);
				int index2 = start - i;
				if (index2 < len) {
					ans.append(c[index2]);
				}
			}
		}
		start = numRows - 1;
		while (start < len) {// 最后一行
			ans.append(c[start]);
			start = start + ((numRows - 1) << 1);
		}
		return ans.toString();
	}

	@Override
	public void debug1() {
		String s = "PAYPALISHIRING";
		int numRows = 3;
		String e = "PAHNAPLSIIGYIR";
		String r = this.convert(s, numRows);
		HelpDebug.compare(e, r);
	}

	@Override
	public void debug2() {
		String s = "PAYPALISHIRING";
		int numRows = 4;
		String e = "PINALSIGYAHRPI";
		String r = this.convert(s, numRows);
		HelpDebug.compare(e, r);
	}

	@Override
	public void debug3() {
		String s = "A";
		int numRows = 1;
		String e = "A";
		String r = this.convert(s, numRows);
		HelpDebug.compare(e, r);
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution6 so = new Solution6();
//		so.debug1();
//		so.debug2();
		so.debug3();
		so.debug4();
	}

}
